﻿define(['app'], function (app) {
    app.controller('borrowerController', ['$scope', '$rootScope', '$timeout', '$http', '$q', 'borrowerService', '$state', 'ngTableParams',
       function ($scope, $rootScope, $timeout, $http, $q, borrowerService, $state, ngTableParams) {

           //代码书写步骤
           //1.用【$scope】定义公共类model,和页面绑定的model
           //2.用【var】定义controller内部用的变量
           //3.用【$scope】和【var】定义方法，注意区分两者定义的方法的区别
           //4.最后定义【init】初始化方法，并调用，进行页面的初始化加载
           //定义规则，按可用性分忧优先级，公共的在前，private的在后

           //初始化listModel
           $scope.tableListData = [];
           $scope.filter = {};
           $scope.model = {};

           $scope.getFirst = function (params) {
               if (params.total() <= (params.page() - 1) * params.count()) {
                   return Math.floor(params.total() / params.count()) * params.count() + 1;
               }
               else {
                   return (params.page() - 1) * params.count() + 1;
               }
           };

           /**
            * 查询tableList数据
            * @return {[object]} [返回table列表数据]
            */
           $scope.tableParams = new ngTableParams({ sorting: { 'id': 'desc' } }, {
               getData: function (params) {
                   // $scope.global.isBusy = true;//暂时没有
                   if ($scope.filter.borrowerName=="") {
                       $scope.filter.borrowerName = null;
                   }
                   return borrowerService.query(params, $scope.filter).then(
                       function (response) {
                           //console.log(response);
                           //// $scope.global.isBusy = false;
                           //if (response.success) {
                           $scope.first = $scope.getFirst(params);
                           var list = response.data.list;
                           //if (list != null) {
                           //    for (var i = 0; i < list.length; i++) {
                           //        list[i].cTime = new Date(list[i].cTime);
                           //        list[i].uTime = new Date(list[i].uTime);
                           //    }
                           //}
                           return list;
                           //} else {
                           //    console.log(response);
                           //    alert("查询数据失败!");
                           //}
                       },
                       function (code) {
                           alert("系统出现问题了！");
                       });
               }
           });

           $scope.reflash = function () {
               $scope.tableParams.page(1);
               $scope.tableParams.reload();
           };
           /**
            * [removeTableList 删除选中行数据]
            * @param  {[type]} bookId [description]
            * @return {[type]}        [description]
            */
           $scope.remove = function (id) {
               // $scope.global.isBusy = true;
               console.log(id);
               if (confirm("确定删除吗？")) {
                   borrowerService.remove(id).then(
                       function (response) {
                           // $scope.global.isBusy = false;
                           if (response.status == 500) {
                               alert(response.message);
                           }
                           else {
                               $scope.reflash();
                           }
                       },
                       function (code) {
                           alert("系统出现问题了！");
                       }
                   );
               }
           };

           $scope.edit = function (item) {
               // $scope.global.isBusy = true;//暂时没有
               if (item == null) {
                   $state.go("book.borrower.edit");
               } else {
                   $state.go("book.borrower.edit", { 'id': item.id });
               }
           };


           var init = function () {
               console.log("grid init");
               $scope.dashboardModel.isActiveMenu = 'borrower';//左侧菜单按钮赋初值
               // $scope.query();
           };

           init();
       }]);

});